package com.hyzx.qbasic.admin.domain.repository.impl;

import com.hyzx.qbasic.admin.model.po.ProcessInstancePO;
import com.hyzx.qbasic.admin.model.po.ProcessTaskInstancePO;
import com.hyzx.qbasic.admin.domain.dao.ProcessInstanceMapper;
import com.hyzx.qbasic.admin.domain.repository.IProcessInstanceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 黄江华 on 2018/11/29
 */

@Repository
public class ProcessInstanceRepositoryImpl implements IProcessInstanceRepository {

    @Autowired
    private ProcessInstanceMapper processInstanceMapper;

    @Override
    @Transactional
    public boolean startProcessInstance(ProcessInstancePO processInstance, ProcessTaskInstancePO firstTask) {
        //如果一个业务已启动了多个流程实例，先删除旧的流程实例
        deleteProcessInstance(processInstance.getBusinessKey());

        boolean success = addProcessInstance(processInstance);
        if (success && firstTask != null) {
            firstTask.setProcessInstanceId(processInstance.getProcessInstanceId());
            addProcessInstanceTask(firstTask);
        }

        return success;
    }

    @Override
    public boolean addProcessInstance(ProcessInstancePO processInstance) {
        return processInstanceMapper.addProcessInstance(processInstance) > 0;
    }

    @Override
    public boolean addProcessInstanceTask(ProcessTaskInstancePO task) {
        return processInstanceMapper.addProcessInstanceTask(task) > 0;
    }

    @Override
    public ProcessInstancePO getProcessInstance(int processInstanceId) {
        return processInstanceMapper.getProcessInstance(processInstanceId);
    }

    @Override
    public ProcessInstancePO getProcessInstanceByBusinessKey(String businessKey) {
        return processInstanceMapper.getProcessInstanceByBusinessKey(businessKey);
    }

    @Override
    public boolean editProcessInstance(ProcessInstancePO processInstance) {
        return processInstanceMapper.editProcessInstance(processInstance) > 0;
    }

    @Override
    public boolean deleteProcessInstance(String businessKey) {
        return processInstanceMapper.deleteProcessInstance(businessKey) > 0;
    }

    @Override
    public boolean batchDeleteProcessInstance(List<String> businessKeyList) {
        return processInstanceMapper.batchDeleteProcessInstance(businessKeyList)>0;
    }

    @Override
    public ProcessTaskInstancePO getProcessTaskInstance(int taskInstanceId) {
        return processInstanceMapper.getProcessTaskInstance(taskInstanceId);
    }

    @Override
    public boolean editProcessTaskInstance(ProcessTaskInstancePO taskInstance) {
        return processInstanceMapper.editProcessTaskInstance(taskInstance) > 0;
    }

    @Override
    @Transactional
    public boolean completeTask(ProcessInstancePO processInstance, ProcessTaskInstancePO currentTask, ProcessTaskInstancePO nextTask) {
        boolean success = editProcessTaskInstance(currentTask);
        if (success) {
            if (nextTask != null) {
                addProcessInstanceTask(nextTask);
            }

            editProcessInstance(processInstance);
        }

        return success;
    }

    @Override
    public List<ProcessTaskInstancePO> getProcessTaskInstanceByBusinessKey(String businessKey) {
        return processInstanceMapper.getProcessTaskInstanceByBusinessKey(businessKey);
    }
}
